<?php
/**
 * 浦发银行
 */
defined('ByShopWWI') or exit('Access Invalid!');
require_once(BASE_ROOT_PATH."/java/Java.inc");
java_require("spdbmerchant.jar;bcprov-jdk14-127.jar;jsse.jar;jnet.jar;jcert.jar;jce.jar;properties.jar");
header("content-type:text/html; charset=utf-8");
class spdb{

	private $payment;
	private $order;

    public function __construct($payment_info,$order_info){
    	$this->spdb($payment_info,$order_info);
    }

    public function spdb($payment_info = array(),$order_info = array()){
    	if(!empty($payment_info) and !empty($order_info)){
    		$this->payment	= $payment_info;
    		$this->order	= $order_info;
    	}
    }

	//页面提交方式
	public function submit_form($tranAbbr){
		$pay_amount = $this -> order['api_pay_amount'];
		$return_url = SHOP_SITE_URL."/api/payment/spdb/return_url.php";
		$subject = $this -> encode($this->order['subject']);  //商品名称

		$order_id = 'ORD'.$this -> order['order_list'][0]['order_id'];  //订单号
		$pay_sn = $this -> order['pay_sn'];    // 商品描述
		$dttm  = date("YmdHis");

		/*$plain="TranAbbr=".$tranAbbr."|MasterID=2005615419|MercDtTm=".$dttm."|TermSsn=".$order_id."|OAcqSsn=|OSttDate=|MercCode=983708160009501|TermCode=00000000|TranAmt=";
		$plain.=$pay_amount."|PayBank=vbank|AccountType=U|PayType=1|Subject=".$subject."|Notice=".$pay_sn."|Remark1=emark1|Remark2=Remark2|MercUrl=".$return_url;
		$plain.="|Ip=|SubMercFlag=1|SubMercName=name|SubMercGoodsName=food|ChannelId=4"; //跨行支付*/

		$plain="TranAbbr=".$tranAbbr."|MasterID=2005615419|MercDtTm=".$dttm."|TermSsn=".$order_id."|OAcqSsn=|OSttDate=|MercCode=983708160009501|TermCode=00000000|TranAmt=";
		$plain.=$pay_amount."|Remark1=emark1|Remark2=Remark2|MercUrl=".$return_url;
		$plain.="|Ip=|SubMercFlag=1|SubMercName=name|SubMercGoodsName=food|PayFlag=00";//本行支付

		$signature=$this -> sign($plain);

		$html = '<html><head></head><body>';
		$html .= '<form action="http://124.74.239.32/payment/main" id="pay_form" method=post>';
		$html .= '<input type=hidden name="transName" value="'.$tranAbbr.'">';
		$html .= '<input type=hidden name="Plain" value="'.$plain.'">';
		$html .= '<input type=hidden name="Signature" value="'.$signature.'">';
		$html .= '</form><script type="text/javascript">document.all.pay_form.submit();</script>';
		$html .= '</body></html>';
		echo $html;
		exit;
	}

	/**
	 * 指令提交, 以post方式提交xml到对应的接口url
	 * @param string $xml  需要post的xml数据
	 * @param int $second   url执行超时时间，默认30s
	 */
	public function submit_xml($tranAbbr, $second = 30)
	{
		$pay_amount = $this -> order['api_pay_amount'];
		$return_url = SHOP_SITE_URL."/api/payment/spdb/return_url.php";
		$subject = $this -> order['subject'];  //商品名称

		$order_id = 'ORD'.$this -> order['order_list'][0]['order_id'];  //订单号
		$pay_sn = $this -> order['pay_sn'];    // 商品描述
		$dttm  = date("YmdHis");

		$plain="MercCode=983708160009501|OTranAbbr=KHTH|TermSsn=ORD713";
		//$plain="TranAbbr=KKJQ|MercCode=983708160009701|MerUserId=2005615419|TermSsn=ORD740|MercDtTm=".$dttm."|PayBatch=0|PayCardName=董军|IdType=1";
		//$plain.="|IdNo=320324198602101932|AcctNo=6225220114597610|StorablePan=|MobileNo=13480167159|ValidateDate=|EncodeData=|TranAmt=0.1";

		$signature=$this -> sign($plain);

		$xml = $this -> packXML($tranAbbr, $plain, $signature);
		$url = 'http://124.74.239.32/payment/paygate';
		$ch = curl_init();
		//设置超时
		curl_setopt($ch, CURLOPT_TIMEOUT, $second);

		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,FALSE);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,FALSE);
		//设置header
		curl_setopt($ch, CURLOPT_HEADER, FALSE);
		//要求结果为字符串且输出到屏幕上
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

		//post提交方式
		curl_setopt($ch, CURLOPT_POST, TRUE);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
		//运行curl
		$data = curl_exec($ch);
		//返回结果
		if($data){
			curl_close($ch);
			return $data;
		} else {
			$error = curl_errno($ch);
			curl_close($ch);
			echo ("curl出错，错误码:$error");
		}
	}

	private function packXML($tranAbbr, $sSendPackData, $sSign){
		$head = htmlspecialchars('<?xml version="1.0" encoding="GBK"?>');
		$sb=$head.htmlspecialchars("<packet><transName>").$tranAbbr.htmlspecialchars("</transName><Plain>").$sSendPackData;
		$sb.=htmlspecialchars("</Plain><Signature>").$sSign.htmlspecialchars("</Signature></packet>");
		return $sb;
	}

	//签名, 用于第三方使用自己的私钥对原始数据进行数字签名
	private function sign($plain){
		$merverify = new Java('com.csii.payment.client.core.MerchantSignVerify');
		$signature = $merverify->merchantSignData_ABA($plain);
		return $signature;
	}

	//验签, 用于第三方使用对银行传递来的信息进行校验
	private function verifyPay($sign, $plain){
		$merverify = new Java('com.csii.payment.client.core.MerchantSignVerify');
		$bol_res = $merverify->merchantVerifyPayGate_ABA($sign, $plain);
		return $bol_res;
	}

	//编码
	private function encode($str){
		$net = new Java("java.net.URLEncoder");
		return $net->encode($str,"GBK");
	}

	//解码
	private function decode($str){
		$net = new Java("java.net.URLEncoder");
		return $net->decode($str,"GBK");
	}
}